package com.lmz.order.listener;

import com.lmz.common.TO.mq.SeckillOrderTo;
import com.lmz.order.entity.OrderEntity;
import com.lmz.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@RabbitListener(queues ="order.seckill.order.queue")
@Component
@Slf4j
public class OrderSeckillLintener {

    @Autowired
    private OrderService orderService;

    @RabbitHandler
    public void closeOrderListener(SeckillOrderTo seckillOrderTo, Channel channel, Message message) throws Exception {
        log.info("准备创建秒杀单详细信息");
        try {
            orderService.createSeckillOrder(seckillOrderTo);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            //有异常重新放回队列等待消费
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }


}